package minh.bac.wcbrazil2014.database;

import java.util.ArrayList;

import minh.bac.wcbrazil2014.object.FootBallTeamObj;
import minh.bac.wcbrazil2014.object.VongBangObj;
import minh.bac.wcbrazil2014.object.VongDau;
import minh.bac.wcbrazil2014.object.XepHangObj;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.webkit.WebChromeClient.CustomViewCallback;

public class MySQLiteManager {

	private SQLiteDatabase mDatabase;
	private MySQLiteHelper mySQLiteHelper;

	public MySQLiteManager(Context context) {
		mySQLiteHelper = MySQLiteHelper.getInstance(context);
		mDatabase = mySQLiteHelper.getWritableDatabase();

	}

	public boolean insert(String TABLE_NAME, ContentValues contentValues) {
		if (contentValues == null) {
			return false;
		} else {
			if (TABLE_NAME.equals(MySQLiteHelper.NAME_TABLE_DOIBONG)) {
				if (checkTableNull(TABLE_NAME)) {
					long check = mDatabase.insert(TABLE_NAME, null,
							contentValues);
					if (check != -1) {
						return true;
					}
				} else {
					// select * from tb_doibong where doibong =''
					// "select "+keyCompare+" from " + tableName + " where " +
					// keySearch +" = '" + valueCompare + "'"
					boolean check = checkDifferent(TABLE_NAME,
							MySQLiteHelper.doiBong, MySQLiteHelper.doiBong,
							contentValues.get(MySQLiteHelper.doiBong)
									.toString(),
							contentValues.get(MySQLiteHelper.doiBong)
									.toString());
					if (!check) {
						long checkDT = mDatabase.insert(TABLE_NAME, null,
								contentValues);
						if (checkDT != -1) {
							return true;
						}
					}
				}
			} else if (TABLE_NAME.equals(MySQLiteHelper.NAME_TABLE_XEPHANG)) {
				if (checkTableNull(TABLE_NAME)) {
					long check = mDatabase.insert(TABLE_NAME, null,
							contentValues);
					if (check != -1) {
						return true;
					}
				} else {
					boolean check1 = checkDifferent(TABLE_NAME,
							MySQLiteHelper.tran, MySQLiteHelper.thb,
							contentValues.get(MySQLiteHelper.tran).toString(),
							contentValues.get(MySQLiteHelper.thb).toString());
					boolean check2 = checkDifferent(
							TABLE_NAME,
							MySQLiteHelper.hieuso,
							MySQLiteHelper.diem,
							contentValues.get(MySQLiteHelper.hieuso).toString(),
							contentValues.get(MySQLiteHelper.diem).toString());
					if (!check1 && !check2) {
						long check = mDatabase.insert(TABLE_NAME, null,
								contentValues);
						if (check != -1) {
							return true;
						}
					}
				}
			} else {
				if (checkTableNull(TABLE_NAME)) {
					long check = mDatabase.insert(TABLE_NAME, null,
							contentValues);
					if (check != -1) {
						return true;
					}
				} else {
					boolean check1 = checkDifferent(
							TABLE_NAME,
							MySQLiteHelper.tran,
							MySQLiteHelper.score1,
							contentValues.get(MySQLiteHelper.score1).toString(),
							contentValues.get(MySQLiteHelper.tran).toString());
					boolean check2 = checkDifferent(
							TABLE_NAME,
							MySQLiteHelper.tran,
							MySQLiteHelper.score2,
							contentValues.get(MySQLiteHelper.score2).toString(),
							contentValues.get(MySQLiteHelper.tran).toString());
					if (!check1 && !check2) {
						long check = mDatabase.insert(TABLE_NAME, null,
								contentValues);
						if (check != -1) {
							return true;
						}
					}
				}
			}
		}
		return false;
	}

	public void closeDB() {
		if (mDatabase.isOpen()) {
			mDatabase.close();
		}
	}

	public boolean checkTableNull(String tableName) {
		Cursor cursor = mDatabase.rawQuery("select * from " + tableName, null);
		if (!cursor.moveToFirst()) {
			cursor.close();
			return true;
		}
		cursor.close();
		return false;
	}

	/*
	 * this method is checking differen from key in and data in database
	 */

	public boolean checkDifferent(String tableName, String keySearch,
			String keyCompare, String res, String valueCompare) {
		String query = "select " + keyCompare + " from " + tableName
				+ " where " + keySearch + " = '" + valueCompare + "'";
		Log.d("SQLiteWC", query);
		Cursor cursor = mDatabase.rawQuery(query, null);

		if (cursor.moveToFirst()) {
			String des = cursor.getString(0);
			if (des.equals(res)) {
				cursor.close();
				return true;
			}
			cursor.close();
			return false;
		}
		cursor.close();
		return false;
	}

	/**
	 * this method is get all football team detail from cursor
	 */
	public ArrayList<FootBallTeamObj> getAllFBTeamDTFromCursor(Cursor cursor) {
		ArrayList<FootBallTeamObj> getData = new ArrayList<FootBallTeamObj>();
		if (cursor.moveToFirst())// neu co du lieu
		{
			// cap phat cho getdata

			while (!cursor.isAfterLast())// vong lap den khi het cursor
			{
				String doiBong = "", bang = "", thuMon = "", hauVe = "", tienVe = "", tienDao = "", tuCachThamDu = "", xepHangFiFa = "", lichSuThamDu = "";
				int favorite;
				//
				doiBong = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.doiBong));
				bang = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.bangDau));
				thuMon = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.thuMon));
				hauVe = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.hauVe));
				tienVe = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.tienVe));
				tienDao = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.tienDao));
				tuCachThamDu = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.tuCachThamDu));
				xepHangFiFa = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.xepHangFiFa));
				lichSuThamDu = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.lichSuThamDu));
				favorite = cursor.getInt(cursor
						.getColumnIndex(MySQLiteHelper.favorite));
				// do data co them check nen lay ra se add check = false xu li
				getData.add(new FootBallTeamObj(doiBong, bang, thuMon, hauVe,
						tienVe, tienDao, tuCachThamDu, xepHangFiFa,
						lichSuThamDu, favorite));
				// cursor dich chuyen den dong tiep theo
				cursor.moveToNext();
			}
			cursor.close();
		}
		return getData;
	}

	/**
	 * this method is query all information vongbang from cursor
	 */
	public ArrayList<VongBangObj> getAllVongBangFromCursor(Cursor cursor) {
		ArrayList<VongBangObj> getData = new ArrayList<VongBangObj>();
		if (cursor.moveToFirst())// neu co du lieu
		{
			// cap phat cho getdata

			while (!cursor.isAfterLast())// vong lap den khi het cursor
			{
				String tran = "", ngay = "", gio = "", doi1 = "", imgLinkDoi1 = "", score1 = "", doi2 = "", imgLinkdoi2 = "", score2 = "", diaDiem = "", kenhTV = "", bang = "";

				bang = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.bangDau));
				tran = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.tran));
				ngay = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.ngay));
				gio = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.gio));
				doi1 = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.doi1));
				imgLinkDoi1 = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.imgLinkDoi1));
				score1 = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.score1));
				doi2 = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.doi2));
				imgLinkdoi2 = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.imgLinkdoi2));
				score2 = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.score2));
				diaDiem = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.diaDiem));
				kenhTV = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.kenhTV));
				// do data co them check nen lay ra se add check = false xu li
				getData.add(new VongBangObj(tran, ngay, gio, doi1, imgLinkDoi1,
						score1, doi2, imgLinkdoi2, score2, diaDiem, kenhTV,
						bang));
				// cursor dich chuyen den dong tiep theo
				cursor.moveToNext();
			}
			cursor.close();
		}
		return getData;
	}

	public ArrayList<VongDau> getAllVongDauFromCursor(Cursor cursor) {
		ArrayList<VongDau> getData = new ArrayList<VongDau>();
		if (cursor.moveToFirst())// neu co du lieu
		{
			// cap phat cho getdata

			while (!cursor.isAfterLast())// vong lap den khi het cursor
			{
				String tran = "", ngay = "", gio = "", doi1 = "", imgLinkDoi1 = "", score1 = "", doi2 = "", imgLinkdoi2 = "", score2 = "", diaDiem = "", kenhTV = "", vong = "";

				vong = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.vong));
				tran = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.tran));
				ngay = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.ngay));
				gio = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.gio));
				doi1 = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.doi1));
				imgLinkDoi1 = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.imgLinkDoi1));
				score1 = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.score1));
				doi2 = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.doi2));
				imgLinkdoi2 = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.imgLinkdoi2));
				score2 = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.score2));
				diaDiem = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.diaDiem));
				kenhTV = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.kenhTV));
				// do data co them check nen lay ra se add check = false xu li
				getData.add(new VongDau(tran, ngay, gio, doi1, imgLinkDoi1,
						score1, doi2, imgLinkdoi2, score2, diaDiem, kenhTV,
						vong));
				// cursor dich chuyen den dong tiep theo
				cursor.moveToNext();
			}
			cursor.close();
		}
		return getData;
	}

	public ArrayList<XepHangObj> getAllXepHangFromCursor(Cursor cursor) {
		ArrayList<XepHangObj> getData = new ArrayList<XepHangObj>();
		if (cursor.moveToFirst()) { // Neu co du lieu
			// Cap phat cho getData
			while (!cursor.isAfterLast()) { // Vong lap den khi het cursor
				String bangdau = "", doibong = "", image = "", tran = "", thb = "", hieuso = "", diem = "";

				bangdau = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.bangDau));
				doibong = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.doiBong));
				image = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.image));
				tran = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.tran));
				thb = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.thb));
				hieuso = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.hieuso));
				diem = cursor.getString(cursor
						.getColumnIndex(MySQLiteHelper.diem));
				getData.add(new XepHangObj(bangdau, doibong, image, tran, thb,
						hieuso, diem));
				cursor.moveToNext();
			}
		}
		return getData;
	}

	/**
	 * 
	 */
	public ArrayList<FootBallTeamObj> getAllFB() {
		String sql = " select * from " + MySQLiteHelper.NAME_TABLE_DOIBONG;
		Cursor cursor = mDatabase.rawQuery(sql, null);
		return getAllFBTeamDTFromCursor(cursor);
	}

	/**
	 * this method is query all information FBTDT from 'start' to 'end'
	 */
	public ArrayList<FootBallTeamObj> getInfromationFBTDT(int start, int end) {
		String query = "select * from " + MySQLiteHelper.NAME_TABLE_DOIBONG
				+ " order by bangdau LIMIT" + start + "," + end;
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllFBTeamDTFromCursor(cursor);
	}

	/**
	 * this method is query all information vongbang from start to end
	 */
	public ArrayList<VongBangObj> getAllFromVongBangLM(int start, int end) {
		String query = "select * from " + MySQLiteHelper.NAME_TABLE_VONBANG
				+ " order by bangdau LIMIT" + start + "," + end;
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllVongBangFromCursor(cursor);
	}

	/**
	 * this method is query all information from vongbang
	 */
	public ArrayList<VongBangObj> getAllFromVongBang() {
		String query = "select * from " + MySQLiteHelper.NAME_TABLE_VONBANG
				+ " order by ngay ASC ";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllVongBangFromCursor(cursor);
	}

	/**
	 * this method is query Footballteam detail by name
	 * 
	 * @param nameFB
	 *            is name of football team
	 */
	public ArrayList<FootBallTeamObj> getFootBallTeamByName(String nameFB) {
		String query = " select * from " + MySQLiteHelper.NAME_TABLE_DOIBONG
				+ " where " + MySQLiteHelper.doiBong + " = '" + nameFB + "' ";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllFBTeamDTFromCursor(cursor);
	}

	/**
	 * this method is query Footballteam detail by name
	 * 
	 * @param nameFB
	 *            is name of football team
	 */
	public ArrayList<VongDau> getAllFromVongDau() {
		String query = " select * from " + MySQLiteHelper.NAME_TABLE_VONGDAU
				+ " order by ngay ASC ";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllVongDauFromCursor(cursor);
	}

	/**
	 * this method is get linkimg by name contry
	 * 
	 * @param nameCountry
	 *            is name football team want to get link
	 */
	public String getLinkImgByName(String nameCountry) {
		String imgLink = "";
		Cursor cursor;
		if (nameCountry != null) {
			String query = "select " + MySQLiteHelper.imgLinkDoi1 + " from "
					+ MySQLiteHelper.NAME_TABLE_VONBANG + " where "
					+ MySQLiteHelper.doi1 + " like '%" + nameCountry + "%'";
			cursor = mDatabase.rawQuery(query, null);
			if (cursor.moveToFirst()) {
				while (cursor.moveToNext()) {
					imgLink = cursor.getString(cursor
							.getColumnIndex(MySQLiteHelper.imgLinkDoi1));
					break;
				}

			}

			cursor.close();
		}
		return imgLink;
	}

	/**
	 * this method is get linkimg by name contry
	 * 
	 * @param nameCountry
	 *            is name football team want to get link
	 */
	public String getLinkImgByName2(String nameCountry) {
		String imgLink = "";
		Cursor cursor;
		if (nameCountry != null) {
			String query = "select " + MySQLiteHelper.imgLinkdoi2 + " from "
					+ MySQLiteHelper.NAME_TABLE_VONBANG + " where "
					+ MySQLiteHelper.doi2 + " like '%" + nameCountry + "%'";
			cursor = mDatabase.rawQuery(query, null);
			if (cursor.moveToFirst()) {
				while (cursor.moveToNext()) {
					imgLink = cursor.getString(cursor
							.getColumnIndex(MySQLiteHelper.imgLinkdoi2));
					break;
				}

			}

			cursor.close();
		}
		return imgLink;
	}

	public String getLinkImage(String nameCountry) {
		String imageLink = null;
		Cursor cursor;
		if (nameCountry != null) {
			String query = "SELECT" + MySQLiteHelper.image + "FROM"
					+ MySQLiteHelper.NAME_TABLE_XEPHANG + "WHERE"
					+ MySQLiteHelper.doiBong + "like '%" + nameCountry + "%'";
			cursor = mDatabase.rawQuery(query, null);
			imageLink = cursor.getString(cursor
					.getColumnIndex(MySQLiteHelper.image));
			cursor.close();
		}
		return imageLink;
	}

	/**
	 * this method is query Vongbang order by date
	 * 
	 * @param nameFB
	 *            is name of football team
	 */
	public ArrayList<VongBangObj> getVongBang() {
		String query = " select * from " + MySQLiteHelper.NAME_TABLE_VONBANG
				+ " order by " + MySQLiteHelper.ngay;
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllVongBangFromCursor(cursor);
	}

	/**
	 * this method is query VongDau order by date
	 * 
	 * @param nameFB
	 *            is name of football team
	 */
	public ArrayList<VongDau> getVongDau() {
		String query = " select * from " + MySQLiteHelper.NAME_TABLE_VONGDAU
				+ " order by " + MySQLiteHelper.ngay;
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllVongDauFromCursor(cursor);
	}

	/**
	 * this method is query VongDau order by date and vong = 1/8
	 * 
	 * @param nameFB
	 *            is name of football team
	 */
	public ArrayList<VongDau> getVong18() {
		String query = " select * from " + MySQLiteHelper.NAME_TABLE_VONGDAU
				+ " where " + MySQLiteHelper.vong + " ='Vòng 1/8' "
				+ " order by " + MySQLiteHelper.tran;
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllVongDauFromCursor(cursor);
	}

	/**
	 * this method is query VongDau order by date and vong ban ket
	 * 
	 * @param nameFB
	 *            is name of football team
	 */
	public ArrayList<VongDau> getVongBanKet() {
		String query = " select * from " + MySQLiteHelper.NAME_TABLE_VONGDAU
				+ " where " + MySQLiteHelper.vong + " ='Vòng bán kết' "
				+ " order by " + MySQLiteHelper.tran;
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllVongDauFromCursor(cursor);
	}

	/**
	 * this method is query VongDau order by date and vong tu ket
	 * 
	 * @param nameFB
	 *            is name of football team
	 */
	public ArrayList<VongDau> getVongTuKet() {
		String query = " select * from " + MySQLiteHelper.NAME_TABLE_VONGDAU
				+ " where " + MySQLiteHelper.vong + " ='Vòng tứ kết' "
				+ " order by " + MySQLiteHelper.tran;
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllVongDauFromCursor(cursor);
	}

	/**
	 * this method is query VongDau order by date and vong chung ket
	 * 
	 * @param nameFB
	 *            is name of football team
	 */
	public ArrayList<VongDau> getVong34() {
		String query = " select * from " + MySQLiteHelper.NAME_TABLE_VONGDAU
				+ " where " + MySQLiteHelper.vong + " ='Tranh hạng 3' ";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllVongDauFromCursor(cursor);
	}

	/**
	 * this method is query VongDau order by date and vong chung ket
	 * 
	 * @param nameFB
	 *            is name of football team
	 */
	public ArrayList<VongDau> getVonChungket() {
		String query = " select * from " + MySQLiteHelper.NAME_TABLE_VONGDAU
				+ " where " + MySQLiteHelper.vong + " ='Chung kết' ";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllVongDauFromCursor(cursor);
	}

	/*
	 * this method is query xephang order by diem and bang dau
	 */
	public ArrayList<XepHangObj> getAllXepHang() {
		String query = "SELECT * FROM " + MySQLiteHelper.NAME_TABLE_XEPHANG
				+ " ORDER BY " + MySQLiteHelper.hieuso + " , "
				+ MySQLiteHelper.diem + " DESC ";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllXepHangFromCursor(cursor);
	}

	public ArrayList<XepHangObj> getXepHangBangA() {
		String query = " SELECT * FROM " + MySQLiteHelper.NAME_TABLE_XEPHANG
				+ " WHERE " + MySQLiteHelper.bangDau + "= 'Bảng A'"
				+ " ORDER BY " + MySQLiteHelper.diem + " DESC ";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllXepHangFromCursor(cursor);
	}

	public ArrayList<XepHangObj> getXepHangBangB() {
		String query = "SELECT * FROM " + MySQLiteHelper.NAME_TABLE_XEPHANG
				+ " WHERE " + MySQLiteHelper.bangDau + "='Bảng B'"
				+ " ORDER BY " + MySQLiteHelper.diem + " DESC ";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllXepHangFromCursor(cursor);
	}

	public ArrayList<XepHangObj> getXepHangBangC() {
		String query = " SELECT * FROM " + MySQLiteHelper.NAME_TABLE_XEPHANG
				+ " WHERE " + MySQLiteHelper.bangDau + "= 'Bảng C'"
				+ " ORDER BY " + MySQLiteHelper.diem + " DESC ";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllXepHangFromCursor(cursor);
	}

	public ArrayList<XepHangObj> getXepHangBangD() {
		String query = "SELECT * FROM " + MySQLiteHelper.NAME_TABLE_XEPHANG
				+ " WHERE " + MySQLiteHelper.bangDau + "='Bảng D'"
				+ " ORDER BY " + MySQLiteHelper.diem + " DESC ";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllXepHangFromCursor(cursor);
	}

	public ArrayList<XepHangObj> getXepHangBangE() {
		String query = " SELECT * FROM " + MySQLiteHelper.NAME_TABLE_XEPHANG
				+ " WHERE " + MySQLiteHelper.bangDau + "= 'Bảng E'"
				+ " ORDER BY " + MySQLiteHelper.diem + " DESC ";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllXepHangFromCursor(cursor);
	}

	public ArrayList<XepHangObj> getXepHangBangF() {
		String query = "SELECT * FROM " + MySQLiteHelper.NAME_TABLE_XEPHANG
				+ " WHERE " + MySQLiteHelper.bangDau + "='Bảng F'"
				+ " ORDER BY " + MySQLiteHelper.diem + " DESC ";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllXepHangFromCursor(cursor);
	}

	public ArrayList<XepHangObj> getXepHangBangG() {
		String query = " SELECT * FROM " + MySQLiteHelper.NAME_TABLE_XEPHANG
				+ " WHERE " + MySQLiteHelper.bangDau + "= 'Bảng G'"
				+ " ORDER BY " + MySQLiteHelper.diem + " DESC ";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllXepHangFromCursor(cursor);
	}

	public ArrayList<XepHangObj> getXepHangBangH() {
		String query = "SELECT * FROM " + MySQLiteHelper.NAME_TABLE_XEPHANG
				+ " WHERE " + MySQLiteHelper.bangDau + "='Bảng H'"
				+ " ORDER BY " + MySQLiteHelper.diem + " DESC ";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return getAllXepHangFromCursor(cursor);
	}

	/**
	 * update favorite of colum database use
	 * 
	 * @param value
	 *            1 = true , 0 = false
	 */
	public void updateFavorite(String NameFB, int value) {
		String sql = " update " + MySQLiteHelper.NAME_TABLE_DOIBONG + " set "
				+ MySQLiteHelper.favorite + " = " + value + " where "
				+ MySQLiteHelper.doiBong + " = '" + NameFB + "'";
		mDatabase.execSQL(sql);
	}

	/**
	 * get all facorite team
	 * 
	 * 
	 */
	public ArrayList<FootBallTeamObj> getFavoriteTeam() {
		String sql = "select * from " + MySQLiteHelper.NAME_TABLE_DOIBONG
				+ " where " + MySQLiteHelper.favorite + " = 1";
		Cursor cursor = mDatabase.rawQuery(sql, null);
		return getAllFBTeamDTFromCursor(cursor);
	}

	/**
	 * get all facorite team
	 * 
	 * @param nameTeam
	 *            : name team want to query
	 */
	public ArrayList<VongBangObj> getVongBangByName(String nameTeam) {
		String sql = " select * from " + MySQLiteHelper.NAME_TABLE_VONBANG
				+ " where " + MySQLiteHelper.doi1 + " ='" + nameTeam + "' or "
				+ MySQLiteHelper.doi2 + " ='" + nameTeam + "'";
		Cursor cursor = mDatabase.rawQuery(sql, null);
		return getAllVongBangFromCursor(cursor);
	}

	/**
	 * get all facorite team
	 * 
	 * @param nameTeam
	 *            : name team want to query
	 */
	public ArrayList<VongDau> getVongDauByName(String nameTeam) {
		String sql = " select * from " + MySQLiteHelper.NAME_TABLE_VONGDAU
				+ " where " + MySQLiteHelper.doi1 + " ='" + nameTeam + "' or "
				+ MySQLiteHelper.doi2 + " ='" + nameTeam + "'";
		Cursor cursor = mDatabase.rawQuery(sql, null);
		return getAllVongDauFromCursor(cursor);
	}

	/**
	 * delete all table
	 */
	public void deleteRowTable(String tableName) {
		mDatabase.delete(tableName, null, null);
	}
}
